#include "guiparammanagerpanel.h"
#include "createnodedialog.h"
#include "common.h"

#include <wx/colordlg.h>

GuiNodeParamEditPanel::GuiNodeParamEditPanel(wxWindow* parent)
    : wxPanel(parent, wxID_ANY),
    mGuiBaseNode(NULL),
    m_nodetypeEditFinished(false),
    m_IDEditFinished(false),
    m_NameEditFinished(false),
    m_TipFinished(false),
    m_RectFinished(false),
    m_NormalColorFinished(false),
    m_OverColorFinished(false),
    m_ClickColorFinished(false),
    m_BorderColorFinished(false)
{
    InitUI();
}

GuiNodeParamEditPanel::~GuiNodeParamEditPanel()
{

}

// 挂接道岔改变信号槽
void GuiNodeParamEditPanel::sig_ProcessItemChanged(GuiParamManagerPanel *pGuiParamManagerPanel)
{
    sig_item_change.connect(pGuiParamManagerPanel,&GuiParamManagerPanel::slot_ProcessItemChanged);
}

void GuiNodeParamEditPanel::InitUI()
{
    m_nodetypeLabel = new wxStaticText(this, wxID_ANY, wxT("类型:"));
    m_nodetypeText = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
                                    CreateChoiceArray({wxT("按钮")}), wxCB_READONLY);

    m_IDLabel = new wxStaticText(this, wxID_ANY, wxT("ID:"));
    m_IDText = new wxTextCtrl(this, wxID_ANY, "",
                                    wxDefaultPosition, wxDefaultSize,
                                    wxTE_PROCESS_ENTER);

    m_NameLabel = new wxStaticText(this, wxID_ANY, wxT("名称:"));
    m_NameText = new wxTextCtrl(this, wxID_ANY, "",
                                    wxDefaultPosition, wxDefaultSize,
                                    wxTE_PROCESS_ENTER);

    m_TipLabel = new wxStaticText(this, wxID_ANY, wxT("提示语:"));
    m_TipText = new wxTextCtrl(this, wxID_ANY, "",
                                 wxDefaultPosition, wxDefaultSize,
                                 wxTE_PROCESS_ENTER);

    m_RectLabel = new wxStaticText(this, wxID_ANY, wxT("显示范围:"));
    m_RectText = new wxTextCtrl(this, wxID_ANY, "",
                                    wxDefaultPosition, wxDefaultSize,
                                    wxTE_PROCESS_ENTER);

    m_NormalColorLabel = new wxStaticText(this, wxID_ANY, wxT("正常颜色:"));
    m_NormalColorText = new wxTextCtrl(this, wxID_ANY, "",
                                    wxDefaultPosition, wxDefaultSize,
                                    wxTE_READONLY);

    m_OverColorLabel = new wxStaticText(this, wxID_ANY, wxT("鼠标越过颜色:"));
    m_OverColorText = new wxTextCtrl(this, wxID_ANY, "",
                                       wxDefaultPosition, wxDefaultSize,
                                       wxTE_READONLY);

    m_ClickColorLabel = new wxStaticText(this, wxID_ANY, wxT("鼠标点击颜色:"));
    m_ClickColorText = new wxTextCtrl(this, wxID_ANY, "",
                                       wxDefaultPosition, wxDefaultSize,
                                       wxTE_READONLY);

    m_BorderColorLabel = new wxStaticText(this, wxID_ANY, wxT("边框颜色:"));
    m_BorderColorText = new wxTextCtrl(this, wxID_ANY, "",
                                       wxDefaultPosition, wxDefaultSize,
                                       wxTE_READONLY);

    // 布局
    wxFlexGridSizer* mainSizer = new wxFlexGridSizer(15, 2, 5, 5);
    mainSizer->AddGrowableCol(1);
    mainSizer->SetFlexibleDirection(wxBOTH);

    mainSizer->Add(m_nodetypeLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_nodetypeText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_IDLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_IDText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_NameLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_NameText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_TipLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_TipText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_RectLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_RectText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_NormalColorLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_NormalColorText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_OverColorLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_OverColorText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_ClickColorLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_ClickColorText, 1, wxEXPAND | wxALL, 5);

    mainSizer->Add(m_BorderColorLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    mainSizer->Add(m_BorderColorText, 1, wxEXPAND | wxALL, 5);

    SetSizerAndFit(mainSizer);

    m_nodetypeText->Bind(wxEVT_COMBOBOX, &GuiNodeParamEditPanel::OnNodeTypeComboBoxSelected, this);

    m_IDText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnIDTextChanged, this);
    m_IDText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnIDTextKillFocus, this);
    m_IDText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnIDTextEnter, this);

    m_NameText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnNameTextChanged, this);
    m_NameText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnNameTextKillFocus, this);
    m_NameText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnNameTextEnter, this);

    m_TipText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnTipTextChanged, this);
    m_TipText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnTipTextKillFocus, this);
    m_TipText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnTipTextEnter, this);

    m_RectText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnRectTextChanged, this);
    m_RectText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnRectTextKillFocus, this);
    m_RectText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnRectTextEnter, this);

    /*m_NormalColorText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnNormalColorTextChanged, this);
    m_NormalColorText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnNormalColorTextKillFocus, this);
    m_NormalColorText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnNormalColorTextEnter, this);

    m_OverColorText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnOverColorTextChanged, this);
    m_OverColorText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnOverColorTextKillFocus, this);
    m_OverColorText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnOverColorTextEnter, this);

    m_ClickColorText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnClickColorTextChanged, this);
    m_ClickColorText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnClickColorTextKillFocus, this);
    m_ClickColorText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnClickColorTextEnter, this);

    m_BorderColorText->Bind(wxEVT_TEXT, &GuiNodeParamEditPanel::OnBorderColorTextChanged, this);
    m_BorderColorText->Bind(wxEVT_KILL_FOCUS, &GuiNodeParamEditPanel::OnBorderColorTextKillFocus, this);
    m_BorderColorText->Bind(wxEVT_TEXT_ENTER, &GuiNodeParamEditPanel::OnBorderColorTextEnter, this);*/

    m_NormalColorText->Bind(wxEVT_LEFT_DCLICK, &GuiNodeParamEditPanel::OnNormalColorTextMouseEvent, this);
    m_OverColorText->Bind(wxEVT_LEFT_DCLICK, &GuiNodeParamEditPanel::OnOverColorTextMouseEvent, this);
    m_ClickColorText->Bind(wxEVT_LEFT_DCLICK, &GuiNodeParamEditPanel::OnClickColorTextMouseEvent, this);
    m_BorderColorText->Bind(wxEVT_LEFT_DCLICK, &GuiNodeParamEditPanel::OnBorderColorTextMouseEvent, this);
}

void GuiNodeParamEditPanel::OnNormalColorTextMouseEvent(wxMouseEvent& event)
{
    event.Skip();

    wxColour decColor = wxColour(255,255,255,255);
    wxArrayString tmpParams = SplitString(m_NormalColorText->GetValue(),",");
    if(tmpParams.size() == 4)
    {
        int r,g,b,a;
        tmpParams[0].ToInt(&r);
        tmpParams[1].ToInt(&g);
        tmpParams[2].ToInt(&b);
        tmpParams[3].ToInt(&a);

        decColor = wxColour(r,g,b,a);
    }

    wxColour decColour = wxGetColourFromUser(this,
                                             decColor,
                                             wxT("选择目标颜色"));

    if(decColour.IsOk())
    {
        m_NormalColorText->SetValue(wxString::Format("%d,%d,%d,%d",decColour.GetRed(),
                                                                   decColour.GetGreen(),
                                                                   decColour.GetBlue(),
                                                                   decColour.GetAlpha()));

        sig_item_change.emitt(mGuiBaseNode,5,m_lastChangedData["normalcolor"],m_NormalColorText->GetValue());
        m_lastChangedData["normalcolor"]=m_NormalColorText->GetValue();
    }
}

void GuiNodeParamEditPanel::OnOverColorTextMouseEvent(wxMouseEvent& event)
{
    event.Skip();

    wxColour decColor = wxColour(255,255,255,255);
    wxArrayString tmpParams = SplitString(m_OverColorText->GetValue(),",");
    if(tmpParams.size() == 4)
    {
        int r,g,b,a;
        tmpParams[0].ToInt(&r);
        tmpParams[1].ToInt(&g);
        tmpParams[2].ToInt(&b);
        tmpParams[3].ToInt(&a);

        decColor = wxColour(r,g,b,a);
    }

    wxColour decColour = wxGetColourFromUser(this,
                                             decColor,
                                             wxT("选择目标颜色"));

    if(decColour.IsOk())
    {
        m_OverColorText->SetValue(wxString::Format("%d,%d,%d,%d",decColour.GetRed(),
                                                     decColour.GetGreen(),
                                                     decColour.GetBlue(),
                                                     decColour.GetAlpha()));

        sig_item_change.emitt(mGuiBaseNode,6,m_lastChangedData["overcolor"],m_OverColorText->GetValue());
        m_lastChangedData["overcolor"]=m_OverColorText->GetValue();
    }
}

void GuiNodeParamEditPanel::OnClickColorTextMouseEvent(wxMouseEvent& event)
{
    event.Skip();

    wxColour decColor = wxColour(255,255,255,255);
    wxArrayString tmpParams = SplitString(m_ClickColorText->GetValue(),",");
    if(tmpParams.size() == 4)
    {
        int r,g,b,a;
        tmpParams[0].ToInt(&r);
        tmpParams[1].ToInt(&g);
        tmpParams[2].ToInt(&b);
        tmpParams[3].ToInt(&a);

        decColor = wxColour(r,g,b,a);
    }

    wxColour decColour = wxGetColourFromUser(this,
                                             decColor,
                                             wxT("选择目标颜色"));

    if(decColour.IsOk())
    {
        m_ClickColorText->SetValue(wxString::Format("%d,%d,%d,%d",decColour.GetRed(),
                                                     decColour.GetGreen(),
                                                     decColour.GetBlue(),
                                                     decColour.GetAlpha()));

        sig_item_change.emitt(mGuiBaseNode,7,m_lastChangedData["clickcolor"],m_ClickColorText->GetValue());
        m_lastChangedData["clickcolor"]=m_ClickColorText->GetValue();
    }
}

void GuiNodeParamEditPanel::OnBorderColorTextMouseEvent(wxMouseEvent& event)
{
    event.Skip();

    wxColour decColor = wxColour(255,255,255,255);
    wxArrayString tmpParams = SplitString(m_BorderColorText->GetValue(),",");
    if(tmpParams.size() == 4)
    {
        int r,g,b,a;
        tmpParams[0].ToInt(&r);
        tmpParams[1].ToInt(&g);
        tmpParams[2].ToInt(&b);
        tmpParams[3].ToInt(&a);

        decColor = wxColour(r,g,b,a);
    }

    wxColour decColour = wxGetColourFromUser(this,
                                             decColor,
                                             wxT("选择目标颜色"));

    if(decColour.IsOk())
    {
        m_BorderColorText->SetValue(wxString::Format("%d,%d,%d,%d",decColour.GetRed(),
                                                     decColour.GetGreen(),
                                                     decColour.GetBlue(),
                                                     decColour.GetAlpha()));

        sig_item_change.emitt(mGuiBaseNode,8,m_lastChangedData["bordercolor"],m_BorderColorText->GetValue());
        m_lastChangedData["bordercolor"]=m_BorderColorText->GetValue();
    }
}

// 重置所有参数
void GuiNodeParamEditPanel::resetAllData(void)
{
    m_nodetypeText->SetValue("");
    m_IDText->SetValue("");
    m_NameText->SetValue("");
    m_TipText->SetValue("");
    m_RectText->SetValue("");
    m_NormalColorText->SetValue("");
    m_OverColorText->SetValue("");
    m_ClickColorText->SetValue("");
    m_BorderColorText->SetValue("");
}

// 获取所有参数数据
std::map<wxString, wxString> GuiNodeParamEditPanel::GetData() const
{
    std::map<wxString, wxString> data;
    data["type"] = m_nodetypeText->GetValue();
    data["id"] = m_IDText->GetValue();
    data["name"] = m_NameText->GetValue();
    data["tip"] = m_TipText->GetValue();
    data["rect"] = m_RectText->GetValue();
    data["normalcolor"] = m_NormalColorText->GetValue();
    data["overcolor"] = m_OverColorText->GetValue();
    data["clickcolor"] = m_ClickColorText->GetValue();
    data["bordercolor"] = m_BorderColorText->GetValue();

    return data;
}

// 设置参数数据
void GuiNodeParamEditPanel::SetData(const std::map<wxString, wxString>& data)
{
    m_lastChangedData = data;

    if (data.count("type")) m_nodetypeText->SetValue(data.at("type"));
    if (data.count("id")) m_IDText->SetValue(data.at("id"));
    if (data.count("name")) m_NameText->SetValue(data.at("name"));
    if (data.count("tip")) m_TipText->SetValue(data.at("tip"));
    if (data.count("rect")) m_RectText->SetValue(data.at("rect"));
    if (data.count("normalcolor")) m_NormalColorText->SetValue(data.at("normalcolor"));
    if (data.count("overcolor")) m_OverColorText->SetValue(data.at("overcolor"));
    if (data.count("clickcolor")) m_ClickColorText->SetValue(data.at("clickcolor"));
    if (data.count("bordercolor")) m_BorderColorText->SetValue(data.at("bordercolor"));
}

void GuiNodeParamEditPanel::OnNodeTypeComboBoxSelected(wxCommandEvent& event)
{

}

void GuiNodeParamEditPanel::OnIDTextChanged(wxCommandEvent& event)
{
    m_IDEditFinished = true;
}

void GuiNodeParamEditPanel::OnIDTextKillFocus(wxFocusEvent& event)
{
    if(m_IDEditFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,1,m_lastChangedData["id"],m_IDText->GetValue());
        m_lastChangedData["id"]=m_IDText->GetValue();
        m_IDEditFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnIDTextEnter(wxCommandEvent& event)
{
    if(m_IDEditFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,1,m_lastChangedData["id"],m_IDText->GetValue());
        m_lastChangedData["id"]=m_IDText->GetValue();
        m_IDEditFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnNameTextChanged(wxCommandEvent& event)
{
    m_NameEditFinished = true;
}

void GuiNodeParamEditPanel::OnNameTextKillFocus(wxFocusEvent& event)
{
    if(m_NameEditFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,2,m_lastChangedData["name"],m_NameText->GetValue());
        m_lastChangedData["name"]=m_NameText->GetValue();
        m_NameEditFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnNameTextEnter(wxCommandEvent& event)
{
    if(m_NameEditFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,2,m_lastChangedData["name"],m_NameText->GetValue());
        m_lastChangedData["name"]=m_NameText->GetValue();
        m_NameEditFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnTipTextChanged(wxCommandEvent& event)
{
    m_TipFinished = true;
}

void GuiNodeParamEditPanel::OnTipTextKillFocus(wxFocusEvent& event)
{
    if(m_TipFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,3,m_lastChangedData["tip"],m_TipText->GetValue());
        m_lastChangedData["tip"]=m_TipText->GetValue();
        m_TipFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnTipTextEnter(wxCommandEvent& event)
{
    if(m_TipFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,3,m_lastChangedData["tip"],m_TipText->GetValue());
        m_lastChangedData["tip"]=m_TipText->GetValue();
        m_TipFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnRectTextChanged(wxCommandEvent& event)
{
    m_RectFinished = true;
}

void GuiNodeParamEditPanel::OnRectTextKillFocus(wxFocusEvent& event)
{
    if(m_RectFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,4,m_lastChangedData["rect"],m_RectText->GetValue());
        m_lastChangedData["rect"]=m_RectText->GetValue();
        m_RectFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnRectTextEnter(wxCommandEvent& event)
{
    if(m_RectFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,4,m_lastChangedData["rect"],m_RectText->GetValue());
        m_lastChangedData["rect"]=m_RectText->GetValue();
        m_RectFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnNormalColorTextChanged(wxCommandEvent& event)
{
    m_NormalColorFinished = true;
}

void GuiNodeParamEditPanel::OnNormalColorTextKillFocus(wxFocusEvent& event)
{
    if(m_NormalColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,5,m_lastChangedData["normalcolor"],m_NormalColorText->GetValue());
        m_lastChangedData["normalcolor"]=m_NormalColorText->GetValue();
        m_NormalColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnNormalColorTextEnter(wxCommandEvent& event)
{
    if(m_NormalColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,5,m_lastChangedData["normalcolor"],m_NormalColorText->GetValue());
        m_lastChangedData["normalcolor"]=m_NormalColorText->GetValue();
        m_NormalColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnOverColorTextChanged(wxCommandEvent& event)
{
    m_OverColorFinished = true;
}

void GuiNodeParamEditPanel::OnOverColorTextKillFocus(wxFocusEvent& event)
{
    if(m_OverColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,6,m_lastChangedData["overcolor"],m_OverColorText->GetValue());
        m_lastChangedData["overcolor"]=m_OverColorText->GetValue();
        m_OverColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnOverColorTextEnter(wxCommandEvent& event)
{
    if(m_OverColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,6,m_lastChangedData["overcolor"],m_OverColorText->GetValue());
        m_lastChangedData["overcolor"]=m_OverColorText->GetValue();
        m_OverColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnClickColorTextChanged(wxCommandEvent& event)
{
    m_ClickColorFinished = true;
}

void GuiNodeParamEditPanel::OnClickColorTextKillFocus(wxFocusEvent& event)
{
    if(m_ClickColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,7,m_lastChangedData["clickcolor"],m_ClickColorText->GetValue());
        m_lastChangedData["clickcolor"]=m_ClickColorText->GetValue();
        m_ClickColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnClickColorTextEnter(wxCommandEvent& event)
{
    if(m_ClickColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,7,m_lastChangedData["clickcolor"],m_ClickColorText->GetValue());
        m_lastChangedData["clickcolor"]=m_ClickColorText->GetValue();
        m_ClickColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnBorderColorTextChanged(wxCommandEvent& event)
{
    m_BorderColorFinished = true;
}

void GuiNodeParamEditPanel::OnBorderColorTextKillFocus(wxFocusEvent& event)
{
    if(m_BorderColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,8,m_lastChangedData["bordercolor"],m_BorderColorText->GetValue());
        m_lastChangedData["bordercolor"]=m_BorderColorText->GetValue();
        m_BorderColorFinished = false;
    }
    event.Skip();
}

void GuiNodeParamEditPanel::OnBorderColorTextEnter(wxCommandEvent& event)
{
    if(m_BorderColorFinished)
    {
        sig_item_change.emitt(mGuiBaseNode,8,m_lastChangedData["bordercolor"],m_BorderColorText->GetValue());
        m_lastChangedData["bordercolor"]=m_BorderColorText->GetValue();
        m_BorderColorFinished = false;
    }
    event.Skip();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GuiParamManagerPanel::GuiParamManagerPanel(wxWindow *parent)
    : wxPanel(parent)
{
    // 创建主垂直布局
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* mountSizer = new wxBoxSizer(wxVERTICAL);

    m_GuiNodeParamEditPanel = new GuiNodeParamEditPanel(this);

    m_GuiNodeParamEditPanel->sig_ProcessItemChanged(this);

    // 创建DataViewCtrl
    m_dvc = new wxDataViewCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxDV_MULTIPLE);

    // 创建数据存储
    m_store = new wxDataViewListStore();
    m_dvc->AssociateModel(m_store);

    mountSizer->Add(m_GuiNodeParamEditPanel, 1, wxEXPAND);

    panelSizer->Add(m_dvc, 3, wxEXPAND);
    panelSizer->Add(mountSizer, 1, wxEXPAND);

    mainSizer->Add(panelSizer, 1, wxEXPAND);

    SetSizer(mainSizer);

    m_dvc->AppendTextColumn(wxT("类型"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 70);
    m_dvc->AppendTextColumn(wxT("ID"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 70);
    m_dvc->AppendTextColumn(wxT("名称"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 100);
    m_dvc->AppendTextColumn(wxT("提示语"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 150);
    m_dvc->AppendTextColumn(wxT("范围"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 120);
    m_dvc->AppendTextColumn(wxT("正常颜色"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 120);
    m_dvc->AppendTextColumn(wxT("鼠标越过颜色"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 120);
    m_dvc->AppendTextColumn(wxT("鼠标点击颜色"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 120);
    m_dvc->AppendTextColumn(wxT("边框颜色"), m_dvc->GetColumnCount(), wxDATAVIEW_CELL_ACTIVATABLE, 120);

    m_dvc->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &GuiParamManagerPanel::OnRightClick, this);
    m_dvc->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &GuiParamManagerPanel::OnSelectionChanged, this);
}

GuiParamManagerPanel::~GuiParamManagerPanel()
{

}

void GuiParamManagerPanel::OnSelectionChanged(wxDataViewEvent& event)
{
    // 获取当前选中的项目
    wxDataViewItem selectedItem = m_dvc->GetSelection();

    if (selectedItem.IsOk())
    {
        // 获取模型
        wxDataViewModel* model = m_dvc->GetModel();

        std::map<wxString, wxString> data;

        // 获取第一列的值
        wxVariant value;
        model->GetValue(value, selectedItem, 0);
        data["type"] = value.GetString();
        model->GetValue(value, selectedItem, 1);
        data["id"] = value.GetString();
        model->GetValue(value, selectedItem, 2);
        data["name"] = value.GetString();
        model->GetValue(value, selectedItem, 3);
        data["tip"] = value.GetString();
        model->GetValue(value, selectedItem, 4);
        data["rect"] = value.GetString();
        model->GetValue(value, selectedItem, 5);
        data["normalcolor"] = value.GetString();
        model->GetValue(value, selectedItem, 6);
        data["overcolor"] = value.GetString();
        model->GetValue(value, selectedItem, 7);
        data["clickcolor"] = value.GetString();
        model->GetValue(value, selectedItem, 8);
        data["bordercolor"] = value.GetString();

        m_GuiNodeParamEditPanel->SetData(data);

        int nodeid = 0;
        data["id"].ToInt(&nodeid);
        GuiBaseNode *pGuiBaseNode = GuiManager::getSingleton().getBaseNodeByID(nodeid);
        if(pGuiBaseNode != NULL)
        {
            m_GuiNodeParamEditPanel->setGuiBaseNode(pGuiBaseNode);

            //m_GuiNodeParamEditPanel->resetAllData();
        }
    }
}

void GuiParamManagerPanel::OnRightClick(wxDataViewEvent& event)
{
    wxDataViewItem item = event.GetItem();
    if (item.IsOk())
    {
        // 获取第一列的值
        wxVariant value,idvalue;
        m_dvc->GetModel()->GetValue(value, item, 0);
        m_dvc->GetModel()->GetValue(idvalue, item, 1);

        // 创建并显示菜单
        wxMenu menu;
        menu.Append(IDD_PARAMEDIT_DELETENODE,
                    wxString::Format(wxT("删除%s:%s"),value.GetString(),idvalue.GetString()));
        menu.Bind(wxEVT_MENU, &GuiParamManagerPanel::OnDeleteGuiNode, this, IDD_PARAMEDIT_DELETENODE);

        m_dvc->PopupMenu(&menu, event.GetPosition());
    }
    else
    {
        // 创建并显示菜单
        wxMenu menu;
        menu.Append(IDD_PARAMEDIT_ADD_MOUNTNODE,wxT("添加按钮"));

        menu.Bind(wxEVT_MENU, &GuiParamManagerPanel::OnAddNewGuiNode, this, IDD_PARAMEDIT_ADD_MOUNTNODE);

        m_dvc->PopupMenu(&menu, event.GetPosition());
    }

    event.Skip();
}

void GuiParamManagerPanel::OnDeleteGuiNode(wxCommandEvent& event)
{
    wxMessageDialog dialog(
        this,
        wxT("您是否要删除当前结点?"),
        wxT("警告"),
        wxYES_NO | wxICON_QUESTION | wxICON_WARNING
        );

    if (dialog.ShowModal() == wxID_YES)
    {
        wxVariant idvalue;
        m_dvc->GetModel()->GetValue(idvalue, m_dvc->GetSelection(), 1);

        if(GuiManager::getSingleton().deleteBaseNodeByID(idvalue.GetInteger()))
        {
            m_store->DeleteItem(m_store->GetRow(m_dvc->GetSelection()));
            m_GuiNodeParamEditPanel->resetAllData();
        }
    }

    event.Skip();
}

void GuiParamManagerPanel::OnAddNewGuiNode(wxCommandEvent& event)
{
    CreateNodeDialog dlg(this, wxT("创建新的按钮"));
    if (dlg.ShowModal() == wxID_OK)
    {
        wxString nodeName = dlg.GetNodeName();

        GuiButton *tmpButton = GuiManager::getSingleton().addButton(GuiManager::getSingleton().getAllBaseNodes().size(),
                                                                    nodeName,
                                                                    wxRect(0,0,100,50));

        this->addGuiBaseNode(tmpButton);
    }
}

void GuiParamManagerPanel::slot_ProcessItemChanged(GuiBaseNode *pGuiNode,int itemindex,wxString olddata,wxString newdata)
{
    if(pGuiNode == NULL || olddata == newdata || newdata == "")
        return;

    // 获取当前选中项
    wxDataViewItem selected = m_dvc->GetSelection();

    if (selected.IsOk())
    {
        const wxDataViewColumn* column = m_dvc->GetColumn(itemindex);

        m_store->SetValue(wxVariant(newdata), selected, column->GetModelColumn());
        m_store->ItemChanged(selected);
    }

    switch(itemindex)
    {
    case 0:
    {

    }
    break;
    case 1:
    {
        int oldnodeid,newnodeid;
        olddata.ToInt(&oldnodeid);
        newdata.ToInt(&newnodeid);

        if(!GuiManager::getSingleton().updateBaseNodeID(oldnodeid,newnodeid))
        {
            wxMessageBox(wxString::Format(wxT("无法修改结点ID为%d"),newnodeid),wxT("警告"));
        }
    }
    break;
    case 2:
    {
        pGuiNode->setNodeName(newdata);
    }
    break;
    case 3:
    {
        pGuiNode->setTipText(newdata);
    }
    break;
    case 4:
    {
        wxArrayString decparams = SplitString(newdata,",");
        if(decparams.size() == 4)
        {
            int x,y,w,h;
            decparams[0].ToInt(&x);
            decparams[1].ToInt(&y);
            decparams[2].ToInt(&w);
            decparams[3].ToInt(&h);

            pGuiNode->setRect(wxRect(x,y,w,h));
        }
    }
    break;
    case 5:
    {
        wxArrayString decparams = SplitString(newdata,",");
        if(decparams.size() == 4)
        {
            int r,g,b,a;
            decparams[0].ToInt(&r);
            decparams[1].ToInt(&g);
            decparams[2].ToInt(&b);
            decparams[3].ToInt(&a);

            pGuiNode->setNormalColor(wxColour(r,g,b,a));

            GuiButton *pGuiButton = static_cast<GuiButton*>(pGuiNode);
            if(pGuiButton != NULL && !pGuiButton->isSelected())
            {
                pGuiButton->setShowColor(pGuiButton->getNormalColor());
            }
        }
    }
    break;
    case 6:
    {
        wxArrayString decparams = SplitString(newdata,",");
        if(decparams.size() == 4)
        {
            int r,g,b,a;
            decparams[0].ToInt(&r);
            decparams[1].ToInt(&g);
            decparams[2].ToInt(&b);
            decparams[3].ToInt(&a);

            pGuiNode->setOverColor(wxColour(r,g,b,a));
        }
    }
    break;
    case 7:
    {
        wxArrayString decparams = SplitString(newdata,",");
        if(decparams.size() == 4)
        {
            int r,g,b,a;
            decparams[0].ToInt(&r);
            decparams[1].ToInt(&g);
            decparams[2].ToInt(&b);
            decparams[3].ToInt(&a);

            pGuiNode->setClickColor(wxColour(r,g,b,a));
        }
    }
    break;
    case 8:
    {
        wxArrayString decparams = SplitString(newdata,",");
        if(decparams.size() == 4)
        {
            int r,g,b,a;
            decparams[0].ToInt(&r);
            decparams[1].ToInt(&g);
            decparams[2].ToInt(&b);
            decparams[3].ToInt(&a);

            pGuiNode->setBorderColor(wxColour(r,g,b,a));
        }
    }
    break;
    default:
        break;
    }
}

void GuiParamManagerPanel::addGuiBaseNode(GuiBaseNode *pGuiBaseNode)
{
    if(pGuiBaseNode == NULL) return;

    wxVector<wxVariant> decdata;

    switch(pGuiBaseNode->getNodeType())
    {
    case GuiNodeType::GUINODETYPE_BUTTON:
    {
        decdata.push_back(wxT("按钮"));
    }
        break;
    default:
        decdata.push_back(wxT("不知"));
        break;
    }

    decdata.push_back(wxString::Format("%d",pGuiBaseNode->getID()));
    decdata.push_back(pGuiBaseNode->getNodeName());
    decdata.push_back(pGuiBaseNode->getTipText());
    decdata.push_back(wxString::Format("%d,%d,%d,%d",pGuiBaseNode->getRect().GetX(),
                                       pGuiBaseNode->getRect().GetY(),
                                       pGuiBaseNode->getRect().GetWidth(),
                                       pGuiBaseNode->getRect().GetHeight()));

    wxColour pnormalcol,povercol,pclickcol;
    pGuiBaseNode->geColor(&pnormalcol,&povercol,&pclickcol);
    wxColour pbordercol = pGuiBaseNode->getBorderColor();

    decdata.push_back(wxString::Format("%d,%d,%d,%d",pnormalcol.GetRed(),
                                       pnormalcol.GetGreen(),
                                       pnormalcol.GetBlue(),
                                       pnormalcol.GetAlpha()));
    decdata.push_back(wxString::Format("%d,%d,%d,%d",povercol.GetRed(),
                                       povercol.GetGreen(),
                                       povercol.GetBlue(),
                                       povercol.GetAlpha()));
    decdata.push_back(wxString::Format("%d,%d,%d,%d",pclickcol.GetRed(),
                                       pclickcol.GetGreen(),
                                       pclickcol.GetBlue(),
                                       pclickcol.GetAlpha()));
    decdata.push_back(wxString::Format("%d,%d,%d,%d",pbordercol.GetRed(),
                                       pbordercol.GetGreen(),
                                       pbordercol.GetBlue(),
                                       pbordercol.GetAlpha()));

    this->addData(decdata);
}

void GuiParamManagerPanel::addData(const wxVector<wxVariant> data)
{
    m_store->AppendItem(data);
}

